/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.data.Store');
(function () {
	
	/**
	 * An extended Store implementation that provided required parameter checking
	 * before executing the load method
	 * @class Store
	 * @extends Ext.data.Store
	 * @namespace net.autodata.data
	 */
	var Store = Ext.extend(Ext.data.Store, {
		
		/**
		 * @property requiredParams
		 * @type Array<String>
		 */
		requiredParams: undefined,
		
		constructor: function ()
		{
			this.addEvents({'requiredparameterexception': true});
			Store.superclass.constructor.apply(this, arguments);
		},
		
		/**
		 * Do not load the store if one of the required parameters is missing
		 * Any parameters passed in need to be checked along with the base params
		 * @method load
		 * @param {Object} options
		 */
		load: function (options)
		{
			var params = Ext.apply({}, (options||{}).params, this.baseParams);
						
			var hasParams = true;
			Ext.each(this.requiredParams, function (paramName) {
				if (Ext.isEmpty(params[paramName])) {
					hasParams = false;
					return false;
				}
			}, this);
			
			if (!hasParams) {
				//ASC.log(String.format('One or more required parameters are not set: {0}', this.proxy.url));
				this.fireEvent('requiredparameterexception');
				return true;
			} 
			
			//ASC.log(String.format('all required parameters are present: {0}', this.proxy.url));
			
			return Store.superclass.load.apply(this, arguments);
		},
		
		/**
         * @method getRawData
         * @param {String} field A field on your records
         * @param {String/RegExp} value Either a string that the field
         * should begin with, or a RegExp to test against the field.
         * @param {Boolean} anyMatch (optional) True to match any part not just the beginning
         * @param {Boolean} caseSensitive (optional) True for case sensitive comparison
         * @param {Boolean} rawData Return the original raw data object
         * @return {Ext.util.MixedCollection} Returns an Ext.util.MixedCollection of the matched records
        */
        getRawData: function(property, value, anyMatch, caseSensitive) 
        {
            var fn = this.createFilterFn(property, value, anyMatch, caseSensitive);
            return this.getRawDataBy(fn);
        },

        getRawDataBy: function(fn, scope) 
        {
            var rows = [];
            var data = this.snapshot || this.data;

            data.each(function(record) {
                if (fn.call(scope || this, record)) {
                    rows.push(record.json);
                }
            }, this);

            var collection = new Ext.util.MixedCollection(false, data.getKey);
            collection.addAll(rows);
            return collection;
        }
	});
	
	net.autodata.data.Store = Store;	
})();